#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <Python.h>
#include "log.h"

// 1. 定义 Calculator 结构体
typedef struct {
    PyObject_HEAD
    int value; // add 增加值
} Calculator;

// 2. 方法实现
static PyObject *calculator_add(Calculator *self, PyObject *args) {
    int value;
    if (!PyArg_ParseTuple(args, "i", &value)) {
        return NULL;
    }
    logd("add called: +%d\n", value);
    self->value += value;
    Py_RETURN_NONE; // 返回 None
}

static PyObject* calculator_getvalue(Calculator *self, void *closure) {
    return PyLong_FromLong(self->value);
}

// 3. 方法定义表
static PyMethodDef calculator_methods[] = {
    {"add", (PyCFunction)calculator_add, METH_VARARGS, "Add a value"},
    {NULL, NULL, 0, NULL}
};

// 4. 属性定义表
static PyGetSetDef calculator_getset[] = {
    {"value", (getter)calculator_getvalue, NULL, "Current value", NULL},
    {NULL}
};

// 5. 类型定义
static PyTypeObject CalculatorType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "script.Calculator", // 模块名.类名
    .tp_basicsize = sizeof(Calculator),
    .tp_flags = Py_TPFLAGS_DEFAULT,
    .tp_doc = "Hardcore C Calculator",
    .tp_methods = calculator_methods,
    .tp_getset = calculator_getset,
    .tp_new = PyType_GenericNew, // 使用默认的 new 方法
};

#endif // CALCULATOR_H
